﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZX.HIS.IDAL;
using ZX.HIS.DBUtility;
using ZX.HIS.Common;
using System.Collections.Generic;//Please add references
namespace ZX.HIS.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:DrugFrequency
	/// </summary>
	public partial class DrugFrequency:IDrugFrequency
	{
		public DrugFrequency()
		{ }
        #region Depart自写方法
        /// <summary>
        /// 分页显示用药频率表
        /// </summary>
        /// <returns></returns>

        public List<ZX.HIS.Model.DrugFrequency> DrugFrequencyList(int PageIndex, int PageSize)
        {
            StringBuilder SQL = new StringBuilder();
            SQL.AppendLine("SELECT * FROM ( ");
            SQL.AppendLine(" SELECT ROW_NUMBER() OVER (");
            SQL.AppendLine("order by DrugFrequencyId desc");
            SQL.AppendLine(")AS aa, tbDrugFrequency.DrugFrequencyId,tbDrugFrequency.HospitalId,tbDrugFrequency.DrugFrequencyName,tbHospital.HospitalName");
            SQL.AppendLine("  from tbDrugFrequency INNER JOIN tbHospital on tbDrugFrequency.HospitalId=tbHospital.HospitalId ");
            SQL.Append(" ) TT");
            SQL.AppendLine(" WHERE aa between " + ((PageIndex - 1) * PageSize + 1) + " AND " + PageIndex * PageSize);
            return SqlDBhelper.GetList<ZX.HIS.Model.DrugFrequency>(SQL.ToString());
        }
        /// <summary>
        /// 显示用药频率表
        /// </summary>
        /// <returns></returns>
        public List<ZX.HIS.Model.DrugFrequency> DrugFrequencyShow()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("SELECT  tbDrugFrequency.DrugFrequencyId,tbDrugFrequency.HospitalId,tbDrugFrequency.DrugFrequencyName,tbHospital.HospitalName  from tbDrugFrequency INNER JOIN tbHospital on tbDrugFrequency.HospitalId=tbHospital.HospitalId ");
            return SqlDBhelper.GetList<ZX.HIS.Model.DrugFrequency>(sql.ToString());
        }
        /// <summary>
        /// 总条数查询
        /// </summary>
        /// <param name="StrWhere"></param>
        /// <returns></returns>
        public int DrugFrequencyCount()
        {
            StringBuilder SQL = new StringBuilder();
            SQL.AppendLine("SELECT COUNT(*) FROM tbDrugFrequency");

            return SqlDBhelper.ExecuteScalarInt(SQL.ToString());
        }
        /// <summary>
        /// 添加用药频率表
        /// </summary>
        /// <param name="DrugUsageModel"></param>
        /// <returns></returns>
        public int AddDrugFrequency(ZX.HIS.Model.DrugFrequency DrugFrequencyModel)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO tbDrugFrequency(");
            strSql.Append("HospitalId,DrugFrequencyName)");
            strSql.Append("values (");
            strSql.Append("@HospitalId,@DrugFrequencyName)");
            strSql.Append(";SELECT @@IDENTITY");
            SqlParameter[] parameter = { new SqlParameter("@HospitalId", SqlDbType.Int, 4), new SqlParameter("@DrugFrequencyName", SqlDbType.VarChar, 100) };
            parameter[0].Value = DrugFrequencyModel.HospitalId;
            parameter[1].Value = DrugFrequencyModel.DrugFrequencyName;
            return SqlDBhelper.ExecuteNonQuery(strSql.ToString(), parameter);

        }
        /// <summary>
        /// 修改用药频率表
        /// </summary>
        /// <param name="DrugUsageModel"></param>
        /// <returns></returns>
        public int UpdateDrugFrequency(ZX.HIS.Model.DrugFrequency DrugFrequencyModel)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("UPDATE tbDrugFrequency SET ");
            strSql.Append("HospitalId=@HospitalId,");
            strSql.Append("DrugFrequencyName=@DrugFrequencyName");
            strSql.Append(" WHERE DrugFrequencyId=@DrugFrequencyId");
            SqlParameter[] parameters = {
                    new SqlParameter("@HospitalId", SqlDbType.Int,4),
                    new SqlParameter("@DrugFrequencyName", SqlDbType.VarChar,100),
                    new SqlParameter("@DrugFrequencyId", SqlDbType.Int,4)};
            parameters[0].Value = DrugFrequencyModel.HospitalId;
            parameters[1].Value = DrugFrequencyModel.DrugFrequencyName;
            parameters[2].Value = DrugFrequencyModel.DrugFrequencyId;
            return SqlDBhelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 删除用药频率表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteDrugFrequency(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE DROM tbDrugFrequency");
            strSql.Append(" WHERE DrugFrequencyId=@DrugFrequencyId");
            SqlParameter[] parameters = {
                     new SqlParameter("@DrugFrequencyId", SqlDbType.Int,4)
             };
            parameters[0].Value = id;

            return SqlDBhelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }

        #endregion
        #region  BasicMethod

        ///// <summary>
        ///// 得到最大ID
        ///// </summary>
        //public int GetMaxId()
        //{
        //return DbHelperSQL.GetMaxID("DrugFrequencyId", "tbDrugFrequency"); 
        //}

        ///// <summary>
        ///// 是否存在该记录
        ///// </summary>
        //public bool Exists(int DrugFrequencyId)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select count(1) from tbDrugFrequency");
        //    strSql.Append(" where DrugFrequencyId=@DrugFrequencyId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@DrugFrequencyId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = DrugFrequencyId;

        //    return DbHelperSQL.Exists(strSql.ToString(),parameters);
        //}


        ///// <summary>
        ///// 增加一条数据
        ///// </summary>
        //public int Add(ZX.HIS.Model.DrugFrequency model)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("insert into tbDrugFrequency(");
        //    strSql.Append("HospitalId,DrugFrequencyName)");
        //    strSql.Append(" values (");
        //    strSql.Append("@HospitalId,@DrugFrequencyName)");
        //    strSql.Append(";select @@IDENTITY");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4),
        //            new SqlParameter("@DrugFrequencyName", SqlDbType.VarChar,100)};
        //    parameters[0].Value = model.HospitalId;
        //    parameters[1].Value = model.DrugFrequencyName;

        //    object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
        //    if (obj == null)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        return Convert.ToInt32(obj);
        //    }
        //}
        ///// <summary>
        ///// 更新一条数据
        ///// </summary>
        //public bool Update(ZX.HIS.Model.DrugFrequency model)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("update tbDrugFrequency set ");
        //    strSql.Append("HospitalId=@HospitalId,");
        //    strSql.Append("DrugFrequencyName=@DrugFrequencyName");
        //    strSql.Append(" where DrugFrequencyId=@DrugFrequencyId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4),
        //            new SqlParameter("@DrugFrequencyName", SqlDbType.VarChar,100),
        //            new SqlParameter("@DrugFrequencyId", SqlDbType.Int,4)};
        //    parameters[0].Value = model.HospitalId;
        //    parameters[1].Value = model.DrugFrequencyName;
        //    parameters[2].Value = model.DrugFrequencyId;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        ///// <summary>
        ///// 删除一条数据
        ///// </summary>
        //public bool Delete(int DrugFrequencyId)
        //{
			
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from tbDrugFrequency ");
        //    strSql.Append(" where DrugFrequencyId=@DrugFrequencyId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@DrugFrequencyId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = DrugFrequencyId;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}
        ///// <summary>
        ///// 批量删除数据
        ///// </summary>
        //public bool DeleteList(string DrugFrequencyIdlist )
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from tbDrugFrequency ");
        //    strSql.Append(" where DrugFrequencyId in ("+DrugFrequencyIdlist + ")  ");
        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}


        ///// <summary>
        ///// 得到一个对象实体
        ///// </summary>
        //public ZX.HIS.Model.DrugFrequency GetModel(int DrugFrequencyId)
        //{
			
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select  top 1 DrugFrequencyId,HospitalId,DrugFrequencyName from tbDrugFrequency ");
        //    strSql.Append(" where DrugFrequencyId=@DrugFrequencyId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@DrugFrequencyId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = DrugFrequencyId;

        //    ZX.HIS.Model.DrugFrequency model=new ZX.HIS.Model.DrugFrequency();
        //    DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
        //    if(ds.Tables[0].Rows.Count>0)
        //    {
        //        return DataRowToModel(ds.Tables[0].Rows[0]);
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}


        ///// <summary>
        ///// 得到一个对象实体
        ///// </summary>
        //public ZX.HIS.Model.DrugFrequency DataRowToModel(DataRow row)
        //{
        //    ZX.HIS.Model.DrugFrequency model=new ZX.HIS.Model.DrugFrequency();
        //    if (row != null)
        //    {
        //        if(row["DrugFrequencyId"]!=null && row["DrugFrequencyId"].ToString()!="")
        //        {
        //            model.DrugFrequencyId=int.Parse(row["DrugFrequencyId"].ToString());
        //        }
        //        if(row["HospitalId"]!=null && row["HospitalId"].ToString()!="")
        //        {
        //            model.HospitalId=int.Parse(row["HospitalId"].ToString());
        //        }
        //        if(row["DrugFrequencyName"]!=null)
        //        {
        //            model.DrugFrequencyName=row["DrugFrequencyName"].ToString();
        //        }
        //    }
        //    return model;
        //}

        ///// <summary>
        ///// 获得数据列表
        ///// </summary>
        //public DataSet GetList(string strWhere)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select DrugFrequencyId,HospitalId,DrugFrequencyName ");
        //    strSql.Append(" FROM tbDrugFrequency ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///// <summary>
        ///// 获得前几行数据
        ///// </summary>
        //public DataSet GetList(int Top,string strWhere,string filedOrder)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select ");
        //    if(Top>0)
        //    {
        //        strSql.Append(" top "+Top.ToString());
        //    }
        //    strSql.Append(" DrugFrequencyId,HospitalId,DrugFrequencyName ");
        //    strSql.Append(" FROM tbDrugFrequency ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    strSql.Append(" order by " + filedOrder);
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///// <summary>
        ///// 获取记录总数
        ///// </summary>
        //public int GetRecordCount(string strWhere)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select count(1) FROM tbDrugFrequency ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    object obj = DbHelperSQL.GetSingle(strSql.ToString());
        //    if (obj == null)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        return Convert.ToInt32(obj);
        //    }
        //}
        ///// <summary>
        ///// 分页获取数据列表
        ///// </summary>
        //public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("SELECT * FROM ( ");
        //    strSql.Append(" SELECT ROW_NUMBER() OVER (");
        //    if (!string.IsNullOrEmpty(orderby.Trim()))
        //    {
        //        strSql.Append("order by T." + orderby );
        //    }
        //    else
        //    {
        //        strSql.Append("order by T.DrugFrequencyId desc");
        //    }
        //    strSql.Append(")AS Row, T.*  from tbDrugFrequency T ");
        //    if (!string.IsNullOrEmpty(strWhere.Trim()))
        //    {
        //        strSql.Append(" WHERE " + strWhere);
        //    }
        //    strSql.Append(" ) TT");
        //    strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///*
        ///// <summary>
        ///// 分页获取数据列表
        ///// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@tblName", SqlDbType.VarChar, 255),
        //            new SqlParameter("@fldName", SqlDbType.VarChar, 255),
        //            new SqlParameter("@PageSize", SqlDbType.Int),
        //            new SqlParameter("@PageIndex", SqlDbType.Int),
        //            new SqlParameter("@IsReCount", SqlDbType.Bit),
        //            new SqlParameter("@OrderType", SqlDbType.Bit),
        //            new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
        //            };
        //    parameters[0].Value = "tbDrugFrequency";
        //    parameters[1].Value = "DrugFrequencyId";
        //    parameters[2].Value = PageSize;
        //    parameters[3].Value = PageIndex;
        //    parameters[4].Value = 0;
        //    parameters[5].Value = 0;
        //    parameters[6].Value = strWhere;	
        //    return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        //}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

